<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : complaint.class.php   FILE_PATH : \\classes\application\complaint.class.php
 * 投诉举报操作页面
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Tue Nov 06 15:37:42 CST 2007
 */
defined('Base_Use_Sign') or die("this is invalid system!please contact <a href='http://www.shopnc.net'>www.shopnc.net</>.");

class Complaint extends FrameWork {
	
	/**
	 * 构造最终应用于SQL查询的where检索语句
	 *
	 * @param string $condition_str 构造后的SQL检索语句字符串
	 * @return string $condition_str 返回完整的where检索语句字符串
	 */
//	function _conditionWhere($condition_str){
//		if(substr(trim($condition_str),0,3)=="and"){
//			$condition_str = " where " . substr(trim($condition_str),3);
//		}
//		return $condition_str;
//	}
	
	/**
	 * 条件组合
	 *
	 * @param var[] $arr=array('state'=>'','class'=>'','send_receive'=>'','member_id'=>'');
	 * @param state 处理状态 all为全部（不包括未处理的）self_all（包括未处理的） no_hand为未处理，handling为处理中 handed为结束处理(handling_no 处理中未申诉，handling_have 处理中已申诉)
	 * class 类别 complaint为投诉report为举报sell为举报卖家buy为举报买家
	 * @param send_receive 作出或者收到 send为作出receive为收到,member_id 会员ID
	 * @param date 日期限制 recent 最近 history 全部 最近时间为7天
	 * @param type 投诉举报类型 
	 * @return string
	 */
	function getCondition($arr){
		
		if ($arr['state'] == 'no_hand'){
			$sql .=" and c_r_handling_state = '0' ";
		}elseif ($arr['state'] == 'handling_no'){
			$sql .=" and c_r_handling_state = '1' ";
		}elseif ($arr['state'] == 'handling_have'){
			$sql .=" and c_r_handling_state = '2' ";
		}elseif ($arr['state'] == 'handling'){
			$sql .="and ( c_r_handling_state = '1' or c_r_handling_state = '2' )";
		}elseif ($arr['state'] == 'handed'){
			$sql .=" and c_r_handling_state = '3' ";
		}elseif ($arr['state'] == 'all'){
			$sql .="";//and c_r_handling_state <> '0'
		}
		
		if ($arr['class'] == 'complaint'){
			$sql .=" and ( c_r_class = '1' or c_r_class = '2' )";
		}elseif ($arr['class'] == 'report'){
			$sql .=" and ( c_r_class = '3' or c_r_class = '4' )";
		}elseif ($arr['class'] == 'sell'){
			$sql .=" and c_r_class = '1' ";
		}elseif ($arr['class'] == 'buy'){
			$sql .=" and c_r_class = '2' ";
		}
		if ($arr['send_receive'] == 'send'){
			$sql .=" and  member_id = '" . $arr['member_id'] . "' ";
		}elseif ($arr['send_receive'] == 'receive'){
			$sql .=" and  c_r_member_id = '" . $arr['member_id'] . "' ";
		}
		if ($arr['type'] != '' && $arr['type'] != '0'){
			$sql .=" and  c_r_type = '" . $arr['type'] . "' ";
		}
		
		$time = time()-30*24*60*60;
		if ($arr['date'] == 'history'){
			$sql .=" ";
		}elseif ($arr['date'] == 'recent'){
			$sql .=" and  c_r_add_time >= '" . $time . "' ";
		}
		
		return $sql;
	}
	
	/**
	 * 投诉/举报 页面 信息条数统计
	 * @author workflow Develop Team
	 * @param  $state 处理状态 0为处理中，1为结束处理,2为没有处理和处理中的状态
	 * @param  $class 类别 0为投诉 1为举报
	 * @param  $send_receive 作出或者收到 0为作出 1为收到
	 * @param  $member_id 会员ID
	 * @return int
	 */
	function complaintStatistics($state,$class,$send_receive,$member_id){
		
		$sql = "";
		
		if ($state == 0){
			$sql .="where ( c_r_handling_state = '1' or c_r_handling_state = '2' )";
		}elseif ($state == 1){
			$sql .="where c_r_handling_state = '3' ";
		}elseif ($state == 2){
			$sql .="where c_r_handling_state <> '3' ";
		}
		
		if ($class == 0){
			$sql .=" and ( c_r_class = '1' or c_r_class = '2' )";
		}elseif ($class == 1){
			$sql .=" and ( c_r_class = '3' or c_r_class = '4' )";
		}
		
		if ($send_receive == 0){
			$sql .=" and  member_id = '" . $member_id . "' ";
		}elseif ($send_receive == 1){
			$sql .=" and  c_r_member_id = '" . $member_id . "' ";
		}
		
		$count = $GLOBALS['db']->GetList($sql,$obj_page,'complaint_report','count(*) as count');
		return $count[0]['count'];
	}
	
	/**
	 * 投诉，举报列表页面
	 * @author workflow Develop Team
	 * @param  $condition搜索条件,&$obj_page分页类,$field 查找字段
	 * @return bool
	 */
	function getComplaintList($condition,&$obj_page,$field="*"){
		
		$subsql = $this->getCondition($condition);
		$complaint_array = $GLOBALS['db']->GetList($subsql,$obj_page,'complaint_report','*',0,'c_r_add_time desc');
		return $complaint_array;
	}

	/**
	 * 添加 投诉举报 内容
	 * @author workflow Develop Team
	 * @param  $input 内容信息
	 * @return bool
	 */
	function addComplaint($input){
		
		if ($GLOBALS['db']->InsertRow($input, 'complaint_report', 'complaint_report_id')){
			return true;
		}
		return false;
	}
	
	/**
	 * 根据指定ID查找投诉举报信息
	 * @author workflow Develop Team
	 * @param  $complaint_report_id 信息ID
	 * @return array
	 */
	function getComplaintById($complaint_report_id){
		
		$array = $GLOBALS['db']->GetOneRow($complaint_report_id,'complaint_report','complaint_report_id');
		return $array;
	}
	
	/**
	 * 更新投诉举报信息
	 * @author workflow Develop Team
	 * @param  $input 更新信息
	 * @return bool
	 */
	function updateComplaintById($input){
		$id = $input['complaint_report_id'];
		if (intval($id) > 0){
			$rs = $GLOBALS['db']->UpdateRow(intval($id),$input,'complaint_report','complaint_report_id');
			return $rs;
		}else {
			return false;
		}
	}
	
	
	/**
	 * 根据投诉举报ID查找系统对信息的留言
	 * @author workflow Develop Team
	 * @param  $complaint_report_id 信息ID,$c_r_class投诉举报信息类别
	 * @return array
	 */
	function getComplaintMsgById($complaint_report_id,$c_r_class,&$obj_page){
		if ($c_r_class == 1 || $c_r_class == 2){
			$class = 0;
		}else if ($c_r_class == 3 || $c_r_class == 4){
			$class = 1;
		}
		$condition = " and complaint_report_id = '" . $complaint_report_id . "' and r_c_msg_type = '" . $class . "'";
		$subsql = $condition;
		
		$complaint_array = $GLOBALS['db']->GetList($subsql,$obj_page,'complaint_report_message','*',0,'r_c_msg_add desc');
		return $complaint_array;
	}
	
	/**
	 * 添加留言
	 * @author workflow Develop Team
	 * @param  $value_array 信息内容
	 * @return bool
	 */
	function addComplaintMsg($value_array){
		if ($GLOBALS['db']->InsertRow($value_array, 'complaint_report_message', 'r_c_msg_id')){
			return true;
		}
		return false;
	}
	
	/**
	 * 删除投诉举报信息
	 * @author workflow Develop Team
	 * @param  $id 投诉举报信息ID
	 * @return bool
	 */
	function delComplaintById($id){
		if ($id){
			if ($GLOBALS['db']->DelRow($id,'complaint_report','complaint_report_id')){
				return true;
			}
			return false;
		}else {
			return false;
		}
	}
	
}

?>